package com.jxs.android.weiget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 
 * @Author Jiangxs
 * @Date 2011-7-21 上午12:06:24
 * @Des 本类为跑马灯工具类 只要提供给一个要显示的字符串，一个TextView，就可以在这个TextView中实现跑马灯。
 */
public class MarqueeText extends TextView {

	private boolean running = true;
	private String descString = "上传中...";

	public MarqueeText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {// 重写的方法用于接收Handler消息
			super.handleMessage(msg);
			setText(descString.substring(0, msg.what));
		}
	};

	public void stopMarquee() {
		this.running = false;
		setText("上传完毕.");
	}

	public void startMarquee() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (running) {
					for (int i = 1; i <= descString.length(); i++) {
						myHandler.sendEmptyMessage(i);
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}

				}

			}
		}).start();
	}
}